/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysGroupOfNames;
import cn.ibizlab.core.ad.service.SysGroupOfNamesService;
import cn.ibizlab.core.ad.filter.SysGroupOfNamesSearchContext;

/**
 * 实体[SysGroupOfNames] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysGroupOfNamesResource {

    @Autowired
    public SysGroupOfNamesService sysGroupOfNamesService;

    @Autowired
    @Lazy
    public SysGroupOfNamesDTOMapping sysGroupOfNamesDtoMapping;

    @Autowired
    @Lazy
    public SysGroupOfNamesFilterDTOMapping sysGroupOfNamesFilterDtoMapping;


    /**
    * 创建Create GroupOfNames
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysGroupOfNamesDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Create-all')")
    @PostMapping(value = {"sys_group_of_names","sys_group_of_names"})
    public ResponseEntity<SysGroupOfNamesDTO> create
            (@Validated @RequestBody SysGroupOfNamesDTO dto) {
        SysGroupOfNames domain = sysGroupOfNamesDtoMapping.toDomain(dto);
        sysGroupOfNamesService.create(domain);
        SysGroupOfNames rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysGroupOfNamesDtoMapping.toDto(rt));
    }

    /**
    * 获取Get GroupOfNames
    * 
    *
    * @param id id
    * @return ResponseEntity<SysGroupOfNamesDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-Get ")
    @GetMapping("sys_group_of_names/{id}")
    public ResponseEntity<SysGroupOfNamesDTO> getById
            (@PathVariable("id") String id) {
        SysGroupOfNames rt = sysGroupOfNamesService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysGroupOfNamesDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove GroupOfNames
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Remove-all')")
    @DeleteMapping("sys_group_of_names/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysGroupOfNamesService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update GroupOfNames
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysGroupOfNamesDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Update-all')")
    @PutMapping("sys_group_of_names/{id}")
    public ResponseEntity<SysGroupOfNamesDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysGroupOfNamesDTO dto) {
        SysGroupOfNames domain = sysGroupOfNamesDtoMapping.toDomain(dto);
        domain.setId(id);
        sysGroupOfNamesService.update(domain);
        SysGroupOfNames rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysGroupOfNamesDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey GroupOfNames
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-CheckKey ")
    @PostMapping("sys_group_of_names/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysGroupOfNamesDTO dto) {
        SysGroupOfNames domain = sysGroupOfNamesDtoMapping.toDomain(dto);
        Integer rt = sysGroupOfNamesService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft GroupOfNames
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysGroupOfNamesDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-GetDraft ")
    @GetMapping("sys_group_of_names/getdraft")
    public ResponseEntity<SysGroupOfNamesDTO> getDraft
            (@SpringQueryMap SysGroupOfNamesDTO dto) {
        SysGroupOfNames domain = sysGroupOfNamesDtoMapping.toDomain(dto);
        SysGroupOfNames rt = sysGroupOfNamesService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysGroupOfNamesDtoMapping.toDto(rt));
    }

    /**
    * Items GroupOfNames
    * 根据传入的id 查询 id对应单位用户组清单，不传入id时自动取当前用户可见用户组清单，其余可选过滤参数:department_id/filter
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<SysGroupOfNamesDTO>>
    */
    @ApiOperation(value = "Items", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-Items 根据传入的id 查询 id对应单位用户组清单，不传入id时自动取当前用户可见用户组清单，其余可选过滤参数:department_id/filter")
    @PostMapping("sys_group_of_names/{id}/items")
    public ResponseEntity<List<SysGroupOfNamesDTO>> itemsById
            (@PathVariable("id") String id, @Validated @RequestBody SysGroupOfNamesDTO dto) {
        SysGroupOfNames domain = sysGroupOfNamesDtoMapping.toDomain(dto);
        domain.setId(id);
        List<SysGroupOfNames> rt = sysGroupOfNamesService.items(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysGroupOfNamesDtoMapping.toDto(rt));
    }

    /**
    * 保存Save GroupOfNames
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysGroupOfNamesDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Save-all')")
    @PostMapping("sys_group_of_names/save")
    public ResponseEntity<SysGroupOfNamesDTO> save
            (@Validated @RequestBody SysGroupOfNamesDTO dto) {
        SysGroupOfNames domain = sysGroupOfNamesDtoMapping.toDomain(dto);
        sysGroupOfNamesService.save(domain);
        SysGroupOfNames rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysGroupOfNamesDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault GroupOfNames
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysGroupOfNamesDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-FetchDefault ")
    @PostMapping("sys_group_of_names/fetchdefault")
    public ResponseEntity<List<SysGroupOfNamesDTO>> fetchDefault
            (@Validated @RequestBody SysGroupOfNamesFilterDTO dto) {
        SysGroupOfNamesSearchContext context = sysGroupOfNamesFilterDtoMapping.toDomain(dto);
        Page<SysGroupOfNames> domains = sysGroupOfNamesService.searchDefault(context) ;
        List<SysGroupOfNamesDTO> list = sysGroupOfNamesDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchMyOrg GroupOfNames
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysGroupOfNamesDTO>>
    */
    @ApiOperation(value = "查询FetchMyOrg", tags = {"GroupOfNames" },  notes = "SysGroupOfNames-FetchMyOrg ")
    @PostMapping("sys_group_of_names/fetchmyorg")
    public ResponseEntity<List<SysGroupOfNamesDTO>> fetchMyOrg
            (@Validated @RequestBody SysGroupOfNamesFilterDTO dto) {
        SysGroupOfNamesSearchContext context = sysGroupOfNamesFilterDtoMapping.toDomain(dto);
        Page<SysGroupOfNames> domains = sysGroupOfNamesService.searchMyOrg(context) ;
        List<SysGroupOfNamesDTO> list = sysGroupOfNamesDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建GroupOfNames
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Create-all')")
    @ApiOperation(value = "批量新建GroupOfNames", tags = {"GroupOfNames" },  notes = "批量新建GroupOfNames")
	@PostMapping("sys_group_of_names/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysGroupOfNamesDTO> dtos) {
        sysGroupOfNamesService.createBatch(sysGroupOfNamesDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除GroupOfNames
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Remove-all')")
    @ApiOperation(value = "批量删除GroupOfNames", tags = {"GroupOfNames" },  notes = "批量删除GroupOfNames")
	@DeleteMapping("sys_group_of_names/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysGroupOfNamesService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新GroupOfNames
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Update-all')")
    @ApiOperation(value = "批量更新GroupOfNames", tags = {"GroupOfNames" },  notes = "批量更新GroupOfNames")
	@PutMapping("sys_group_of_names/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysGroupOfNamesDTO> dtos) {
        sysGroupOfNamesService.updateBatch(sysGroupOfNamesDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存GroupOfNames
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Save-all')")
    @ApiOperation(value = "批量保存GroupOfNames", tags = {"GroupOfNames" },  notes = "批量保存GroupOfNames")
	@PostMapping("sys_group_of_names/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysGroupOfNamesDTO> dtos) {
        sysGroupOfNamesService.saveBatch(sysGroupOfNamesDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入GroupOfNames
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGroupOfNames-Save-all')")
    @ApiOperation(value = "批量导入GroupOfNames", tags = {"GroupOfNames" },  notes = "批量导入GroupOfNames")
	@PostMapping("sys_group_of_names/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysGroupOfNamesDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysGroupOfNamesService.importData(config,ignoreError,sysGroupOfNamesDtoMapping.toDomain(dtos)));
    }

}
